/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cr.ac.una.sive.managed;

import cr.ac.una.sive.sessionbean.UsuarioSistemaFacadeLocal;
import cr.ac.una.sive.entidadLocal.UsuarioSistemaLocal;
import cr.ac.una.sive.sessionbean.UsuarioFacade;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

/**
 *
 * @author claribel
 */
@ManagedBean
@SessionScoped
public class UsuarioSistemaFacadeManaged {
    @EJB(mappedName = "ejb/UsuarioSistemaFacade")
    private UsuarioSistemaFacadeLocal usuarioSistemaFacade;

     EntityManagerFactory emf=Persistence.createEntityManagerFactory("una-sivePU");
    EntityManager em=emf.createEntityManager();
    
    private UsuarioSistemaLocal usuarioSistemaLocal = new UsuarioSistemaLocal();

    public UsuarioSistemaLocal getUsuarioSistemaLocal() {
        return usuarioSistemaLocal;
    }

    public void setUsuarioSistemaLocal(UsuarioSistemaLocal usuarioSistemaLocal) {
        this.usuarioSistemaLocal = usuarioSistemaLocal;
    }
    
    
    /**
     * Creates a new instance of UsuarioSistemaFacadeManaged
     */
    public UsuarioSistemaFacadeManaged() {
    }
    
    public boolean validaLogin(String user,String pass) {
          FacesMessage message = new FacesMessage();
          FacesContext context = FacesContext.getCurrentInstance();
        try{
            
            TypedQuery<UsuarioSistemaLocal> query = em.createQuery(
            "SELECT c FROM UsuarioSistema c WHERE c.usuarioSistema = '" + user + "' AND c.clave = '" + pass + "'",
            UsuarioSistemaLocal.class);
            List u;
            u = query.getResultList();
            if(u.isEmpty())
                return false;
            else 
                return true;
//            Iterator stIterator=stList.iterator();
//                while(stIterator.hasNext()){
//                Student st=(Student)stIterator.next();
//                System.out.print("sname:"+st.getSname());
//                System.out.print(" sroll:"+st.getSroll());
//                System.out.print(" scourse:"+st.getScourse());
//                System.out.println();
//            }
        }catch(Exception e){
            message.setSeverity(FacesMessage.SEVERITY_INFO);
            message.setSummary(e.getMessage());
            message.setDetail(e.getMessage());     
            context.addMessage("Campos", message);
            return false;
        }
    } 
    public void validate(ActionEvent event){
//           setDisplayName(login);
           FacesMessage message = new FacesMessage();
           FacesContext context = FacesContext.getCurrentInstance();
           boolean valido;
           valido = validaLogin(usuarioSistemaLocal.getUsuarioSistema(),usuarioSistemaLocal.getClave());
        
        if(valido){ 
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(UsuarioSistemaFacadeManaged.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
           
             message.setSeverity(FacesMessage.SEVERITY_INFO);
             message.setSummary("Atención: El usuario no existe");
             message.setDetail("Atención: El usuario no existe");
             context.addMessage("loginForm:login", message);
             
        }    
    }
}
